const noLoginRoutesMap = {
  login: {
    pageKey: 'login',
    name: '登录',
  },
}

const loginNoMenuMap = {
  '3dScreen': {
    pageKey: '3dScreen',
    name: '大屏',
  },
}

export const loginRoutesMap = {
  //-----------------------通用-------------------------//
  home: {
    pageKey: 'home',
    name: '首页',
  },
  personalCenter: {
    pageKey: 'personalCenter',
    name: '个人中心',
  },
  hospitalManagement: {
    pageKey: 'hospitalManagement',
    name: '机构管理',
  },
  systemFeature: {
    pageKey: 'systemFeature',
    name: '功能列表',
  },
  roleManage: {
    pageKey: 'roleManage',
    name: '角色管理',
  },
  organization: {
    pageKey: 'organization',
    name: '部门管理',
  },
  qualificationType: {
    pageKey: 'qualificationType',
    name: '资质类型',
  },
  menuManage: {
    pageKey: 'menuManage',
    name: '菜单管理',
  },
  customForm: {
    pageKey: 'customForm',
    name: '自定义表单',
  },
  //-----------------------陪护管理-------------------------//
  pcsHospital: {
    pageKey: 'pcsHospital',
    name: '医院管理',
    dependsChildren: {
      pcsHospitalArea: {
        pageKey: 'pcsHospitalArea',
        name: '院区管理',
      },
    },
  },
  pcsInpatientWard: {
    pageKey: 'pcsInpatientWard',
    name: '病区管理',
  },
  pcsCompanionCompany: {
    pageKey: 'pcsCompanionCompany',
    name: '陪护公司管理',
  },
  pcsCompanionContent: {
    pageKey: 'pcsCompanionContent',
    name: '陪护内容',
  },
  pcsCompanionFlow: {
    pageKey: 'pcsCompanionFlow',
    name: '陪护流程',
  },
  pcsCompanionCombo: {
    pageKey: 'pcsCompanionCombo',
    name: '陪护套餐',
  },
  pcsCompanionOrder: {
    pageKey: 'pcsCompanionOrder',
    name: '陪护订单',
  },
  pcsNursingLifeLogWord: {
    pageKey: 'pcsNursingLifeLogWord',
    name: '生活日志结语',
  },
  pcsNursingWorkers: {
    pageKey: 'pcsNursingWorkers',
    name: '陪护人员',
  },
  pcsNursingItem: {
    pageKey: 'pcsNursingItem',
    name: '陪护项目',
  },
  pcsNursingType: {
    pageKey: 'pcsNursingType',
    name: '护嘱分类',
  },
  feedback: {
    pageKey: 'feedback',
    name: '需求反馈',
  },
  cyCategory: {
    pageKey: 'cyCategory',
    name: '商品分类',
  },
  commodity: {
    pageKey: 'commodity',
    name: '商品管理',
  },
  gwOrders: {
    pageKey: 'gwOrders',
    name: '商品订单',
  },
  insurance: {
    pageKey: 'insurance',
    name: '长护险管理',
  },
  pcsCostStatistics: {
    pageKey: 'pcsCostStatistics',
    name: '费用统计',
  },
  companionFeedback: {
    pageKey: 'companionFeedback',
    name: '反馈与建议',
  },
  pcsStatementManage: {
    pageKey: 'pcsStatementManage',
    name: '结算管理',
    dependsChildren: {
      pcsStatementDetail: {
        pageKey: 'pcsStatementDetail',
        name: '结算详情',
      },
    },
  },
  pcsOrderStatistics: {
    pageKey: 'pcsOrderStatistics',
    name: '订单统计',
  },
  pcsBillManage: {
    pageKey: 'pcsBillManage',
    name: '账单管理',
  },
}

const getRoutes = routesMap => {
  const routes = []
  const deepGetRoutes = (_routesMap, { path: parentPath = '', pageDesc: parentPageDesc = '' } = {}) => {
    Object.values(_routesMap).forEach(item => {
      const { pageKey, name, defaultIcon, dependsChildren } = item
      const path = `${parentPath}/${pageKey}`
      const pageDesc = `${parentPageDesc} ${name}`.trimStart()
      routes.push({
        pageKey,
        name,
        defaultIcon,
        path,
        pageDesc,
      })
      if (dependsChildren) {
        deepGetRoutes(dependsChildren, {
          path,
          pageDesc,
        })
      }
    })
  }
  deepGetRoutes(routesMap)
  return routes
}

export const noLoginRoutes = getRoutes(noLoginRoutesMap)
export const loginRoutes = getRoutes(loginRoutesMap)
export const loginNoMenuRoutes = getRoutes(loginNoMenuMap)

console.debug('noLoginRoutes', noLoginRoutes)
console.debug('loginRoutes', loginRoutes)
console.debug('loginNoMenuRoutes', loginNoMenuRoutes)

/**
 * 例：[{
 *  name: "院区管理"
 *  pageDesc: "医院管理 院区管理"
 *  pageKey: "pcsHospitalArea"
 *  path: "/pcsHospital/pcsHospitalArea"
 * }]
 */

const getRoutesMap = () => {
  const routesMap = {}
  const allRoutes = [...noLoginRoutes, ...loginRoutes, ...loginNoMenuRoutes]
  allRoutes.forEach(({ pageKey, path, name, pageDesc, defaultIcon }) => {
    if (routesMap[pageKey]) {
      throw Error('重复配置相同key的路由')
    }
    routesMap[pageKey] = {
      pageKey,
      path,
      name,
      pageDesc,
      defaultIcon,
    }
  })
  return routesMap
}

/**
 * 例：{
 *  pcsHospitalArea:{
 *    pageKey: "pcsHospitalArea"
 *    path: "/pcsHospital/pcsHospitalArea"
 *  }
 * }
 */
export const routesMap = getRoutesMap()

console.debug('routesMap', routesMap)

const getMenus = routes => {
  const deepGetMenus = _routesMap => {
    const menus = []
    Object.values(_routesMap).forEach(item => {
      let list = undefined
      if (item.dependsChildren && !item.hideChildMenu) {
        list = deepGetMenus(item.dependsChildren)
      }
      const menu = {
        key: item.pageKey,
        name: item.name,
      }
      if (list) {
        menu.list = list
      }
      menus.push(menu)
    })
    return menus
  }
  return deepGetMenus(routes)
}

export const devMenus = getMenus(loginRoutesMap)
